/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLDecoder;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * 
 */
public class AppConfig {
    private String path="";
    private String filename="";
    private final String sep="=";
    private final String sep1=",";

    public AppConfig() {
        
        filename="AppConfig.properties";
        SetAppPathConfig();
    }
    
    public AppConfig(String filename){
    	
    	this.filename=filename;
    }
    
    public AppConfig(HttpServletRequest req,String filename){
    	this.filename=filename;
    	SetWebPath(req);
    }
    
    public String getFilename(){
    	return filename;
    }
    
    public void setFilename(String filename){
    	
    	this.filename=filename;
    }
    /**
     * run again web path if want run with web app
     * */
    public void runWebPath(HttpServletRequest req){
    	SetWebPath(req);
    }
    
    public void runAppPath(){
    	
    	SetAppPathConfig();
    }

    public String read(String needle){
        String str="";
        try {
            //FileInputStream fs=new FileInputStream(this.path);
            //BufferedInputStream bs=new BufferedInputStream(fs);
            //DataInputStream ds=new DataInputStream(bs);
            FileReader fr=new FileReader(this.path);
            BufferedReader br=new BufferedReader(fr);
            str=br.readLine();
            while(str!=null){
                //System.out.println(str);
                if(str.indexOf(needle)!=-1){
                    String[] arr=str.split(this.sep);
                    if(arr.length > 1){
                        return arr[1].split(this.sep1)[0].trim();
                    }
                }
                str=br.readLine();
            }
            //fs.close();
            //bs.close();
            fr.close();
            br.close();
        } catch (FileNotFoundException e) {
            Logger.LogError("AppConfig",e.toString() );
            //return e.toString();
        }
        catch(IOException e){
            Logger.LogError("AppConfig",e.toString() );
            //return e.toString();
        }
        
        return "";
    }

    public String[] readArray(String needle){
        try {
            String str="";
            FileReader fr=new FileReader(this.path);
            BufferedReader br=new BufferedReader(fr);
            str=br.readLine();
            while(str!=null){
                //System.out.println(str);
                if(str.indexOf(needle)!=-1){
                    String[] arr=str.split(this.sep);
                    if(arr.length > 1){
                        String strarr=arr[1].replaceAll(" ", "");
                        return strarr.split(this.sep1);
                    }
                }
                str=br.readLine();
            }
            //fs.close();
            //bs.close();
            fr.close();
            br.close();
        } catch (FileNotFoundException e) {
            Logger.LogError("AppConfig",e.toString() );
        }
        catch(IOException e){
            Logger.LogError("AppConfig",e.toString() );
        }

        return null;
    }

    private void SetAppPathConfig(){
        String xpath=this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        this.path=xpath+this.filename;
        this.path=URLDecoder.decode(this.path);
    }
    
    private void SetWebPath(HttpServletRequest req){
    	String xpath=req.getRealPath("");
        this.path=xpath+"/"+this.filename;
        this.path=URLDecoder.decode(this.path);
    }
}
